package com.web.controller;

import com.web.model.Register;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * @author yangguanzan
 * @date 9/21/21
 * @description
 */
@Controller
@RequestMapping("/hospital/register")
public class RegisterController {

    private static final Logger LOGGER= LoggerFactory.getLogger(RegisterController.class);

    @GetMapping(value =  {"/list"} )
    public String list(ModelMap model,Integer pageNo,String name) {
        System.out.println(name);
        List<Register> list = new ArrayList<>();
        Register register = new Register();
        register.setId(1L);
        register.setStatus(0);
        register.setDoctorId(1L);
        register.setRegisterNum("123456");
        register.setDepartment("急诊科");
        register.setName("张三");
        register.setVisitDt(new Date());
        list.add(register);

        register = new Register();
        register.setId(2L);
        register.setStatus(1);
        register.setDoctorId(2L);
        register.setRegisterNum("3422244");
        register.setDepartment("儿科");
        register.setName("李四");
        register.setVisitDt(new Date());
        list.add(register);

        register = new Register();
        register.setId(3L);
        register.setStatus(3);
        register.setDoctorId(2L);
        register.setRegisterNum("34534534");
        register.setDepartment("内科");
        register.setName("五四");
        register.setVisitDt(new Date());
        list.add(register);

        register = new Register();
        register.setId(4L);
        register.setStatus(4);
        register.setDoctorId(2L);
        register.setRegisterNum("5675677");
        register.setDepartment("牙科");
        register.setName("王五");
        register.setVisitDt(new Date());
        list.add(register);

        int pageSize = 2;
        int total = list.size();
        int startNo= (pageNo.intValue()-1)*pageSize;
        if(startNo<0||startNo>total){
            startNo =0;
        }
        int endNo = pageNo.intValue()*pageSize;
        if(endNo>total){
            endNo=total;
        }
        model.addAttribute("list",list.subList(startNo,endNo));
        model.addAttribute("total",total);
        model.addAttribute("pageTotal",(int)Math.ceil(Double.valueOf(total)/pageSize));

        return "register/table";
    }

    @GetMapping(value =  {"/",""} )
    public String index() {

        return "register/index";
    }

    @ResponseBody
    @GetMapping(value =  {"/deleteById"} )
    public Map deleteById(Long id) {
        LOGGER.info("挂号信息删除ID:{}",id);
        Map map = new HashMap(1);
        map.put("msg","删除成功！");
        return map;
    }

    @ResponseBody
    @GetMapping(value =  {"/delete"} )
    public Map delete(String ids) {
        LOGGER.info("挂号信息批量删除IDS:{}",ids);
        Map map = new HashMap(1);
        map.put("msg","批量删除成功！");
        return map;
    }

    @GetMapping(value =  {"/add"} )
    public String add() {
        return "register/add";
    }


}
